<?php
class Merchant_Service_QualityControlOrder
{
	public static $statusText = array(
		"0" => "草稿",
		"1" => "确认",
		"2" => "完成",
		"3" => "异常",
		"4" => "删除",
	);
	public static function getByCond($cond,$type,$order,$count,$offset){
		$Obj = new Merchant_Model_QualityControlOrder();
		$rows = $Obj->getByCond($cond,$type,$order,$count,$offset);
		return $rows;
	}

	public static function getByCondition($condition=array(),$type='*',$order=array('qco_id'), $pageSize=0, $page=1)
	{ 
		if(isset($condition["date_from"]) && $condition['date_from'] != ""){
			$condition['date_from']=date('Y-m-d',strtotime("{$condition['date_from']}"))." 00:00:00"; 
		}
		if(isset($condition["date_to"]) && $condition['date_to'] != ""){
			$condition['date_to']=date('Y-m-d',strtotime("{$condition['date_to']}"))." 23:59:59"; 
		}
		$obj=new Merchant_Model_QualityControlOrder();
		$rows= $obj->getByCondition( $condition,$type,$order, $pageSize,$page);
		if(is_array($rows)){
			foreach($rows as $key => $val){
				$rows[$key]["statusText"] = self::$statusText[$val['qco_status']];
				$userRow=User_Service_User::getByUserId($val['user_id']);
				$rows[$key]["user_id"] = $userRow['name'];
			}
		}
		return $rows;
	}
	
	public function getAdapter(){
		$model = new Merchant_Model_QualityControlOrder();
		return $model->getAdapter();
	}
	
	public static function add($row){
		$model = new Merchant_Model_QualityControlOrder();
		return $model->add($row);
	}
	
	public static function update($row,$id){
		$model = new Merchant_Model_QualityControlOrder();
		return $model->update($row,$id);
	}
	
	public static function delete($qco_id){
		$model = new Merchant_Model_QualityControlOrder();
		return $model->delete($qco_id);
	}
	
	public static function deleteByQcoCode($qco_code){
		$model = new Merchant_Model_QualityControlOrder();
		$itemModel = new Merchant_Model_QualityControlItem();
		$result1=$itemModel->deleteByQcoCode($qco_code);
		$result2=$model->deleteByQcoCode($qco_code);
		if($result1 && $result2){
			return false;
		}
		return true;
	}
	
	public static function getQcoCode($qco_code){
		$model = new Merchant_Model_QualityControlOrder();
		$row=$model->getQcoCode($qco_code);
		if(!empty($row)){
			return $row->toArray();
		}
		return $row;
	}
	
	/*
	*根据参考号及产品ID获取通过QC的数量
	*param qco_ref_code product_id
	*retrun int 
	*/
	public static function getByQcoRefCodeAndProductId($qco_ref_code=null,$product_id=0){
		if($qco_ref_code=='' || $product_id==0 ){
			return 0;
		}
		$qc_count=0;//通过数量
		$QcAll= self::getByCondition(array('qco_ref_code'=>$qco_ref_code,'qco_status'=>2),'*','',0,0);
		if(!empty($QcAll)){
			foreach($QcAll as $key => $val){
				$itemAll=Merchant_Service_QualityControlItem::getByCondition(array('qco_code'=>$val['qco_code'],'product_id'=>$product_id,'qci_status'=>3),'*','');
				foreach($itemAll as $k => $v){
					$qc_count+=$v['qci_quantity_pass'];
				}
			}
		}
		return $qc_count;
	}
	
	/*
	*根据参考号获取信息
	*/
	public static function getByQcoRefCode($qco_ref_code){
		$model = new Merchant_Model_QualityControlOrder();
		$res=$model->getByQcoRefCode($qco_ref_code);
		if(!empty($res)){
			return $res->toArray();
		}
		return $res;
	}
	
	/*
	 * 获取当天最后一个 qco_code
	 */
	public static function getCurdateLastCode()
	{ 
		$obj=new Merchant_Model_QualityControlOrder();
		$rows=$obj->getCurdateLastCode();
		$QCNO = 'Q'.date('ymd');
		$tmp ='';
		if($rows){
			$tmp= (int) substr($rows,-3,3);
			$tmp= str_pad(($tmp+1),3,'0',STR_PAD_LEFT);
		}else{
			$tmp ='001';
		}
		$QCNO.=$tmp;
		return $QCNO;
	}
	
	public static function getDetailResult($qco_code){
		$res = self::getQcoCode($qco_code);
		//获取产品信息 1.QC_CODE -> PRODUCT_ID
		$pidsRes = Merchant_Service_QualityControlItem::getQcoCodeAll($qco_code);
		//PID -> PRODUCT_SKU 
		//PID + PO_ID -> PO_VALUE POC_ID
		//POC_ID -> POC_VLAUE
		$povModel = new Merchant_Model_ProductOptionValue();
		$pocModel = Merchant_Model_ProductOptionChoose::getInstance();
		$qciModel = Merchant_Model_QualityControlItem::getInstance();
		$objConfig = new Default_Model_Config();
		$congifRes = $objConfig->getByValue('QUANTITY_CONTROL_ID');
        $qcrModel = Merchant_Model_QualityControlResult::getInstance();
		foreach($pidsRes as $key=>$val){
			$productRes = Merchant_Service_Product::getByProductId($val['product_id']);
			$povRes = $povModel->getByProductId($val['product_id'],$congifRes['config_value']);//返回多条
			$qciRes = $qciModel->getByQcoCodeAndProductId($qco_code,$val['product_id']);
			$proAttId = Merchant_Service_ProductAttached::getPicturesByPid($val['product_id']);
			$temp = array();
			foreach($povRes as $k=>$v){
				$pocRes = $pocModel->getByPocid($v['poc_id']);
				$qcrRes = $qcrModel->getByPovIdAndQciId($v['pov_id'],$qciRes['qci_id']);
				$temp[] = array(
						'product_sku'=>$productRes['product_sku'],
						'customer_id'=>$productRes['customer_id'],
						'product_weight'=>$productRes['product_weight'],
						'poc_value_cn'=>$pocRes['poc_value_cn'],
						'poc_value'=>$pocRes['poc_value'],
						'po_value'=>$v['po_value'],
						'poc_id'=>$v['poc_id'],
						'qcr_quantity_problem'=>$qcrRes['qcr_quantity_problem']==null?'':$qcrRes['qcr_quantity_problem'],
						'ws_code'=>$qciRes['ws_code'],
						'qci_quantity'=>$qciRes['qci_quantity']==null?'0':$qciRes['qci_quantity'],
						'qci_quantity_problem'=>$qciRes['qci_quantity_problem'] == null ? '' : $qciRes['qci_quantity_problem'],
						'qcr_description'=>$qcrRes['qcr_description']==null?'':$qcrRes['qcr_description'],
						'proAttId'=>$proAttId);
			}
			
			$tempElse[]=array(
					    'product_sku'=>$productRes['product_sku'],
						'customer_id'=>$productRes['customer_id'],
						'product_weight'=>$productRes['product_weight'],
						'poc_value_cn'=>'',
						'poc_value'=>0,
						'po_value'=>0,
						'poc_id'=>0,
						'qcr_quantity_problem'=>0,
						'ws_code'=>0,
						'qci_quantity'=>0,
						'qci_quantity_problem'=>0,
						'qcr_description'=>'',
						'proAttId'=>$proAttId //图片展览
            );
			if(!$temp){
				$temp=$tempElse;
			}
			$res['product_info'][$val['product_id']] = $temp;
			
		}		
		return $res;
	}

	public static function getPicture($productId){
		$attach = new Merchant_Model_ProductAttached();
		$picture = $attach->getViewPicture($productId);
		$pspobj= new Merchant_Service_Product();
		$product = $pspobj->getProductInfo($productId);
		$result = false;
		if($picture instanceof Zend_Db_Table_Row){
			$picture = $picture->toArray();
			if($picture['pa_file_type'] == "img"){
				$result = "../../front/data/images/".$product['customer_id']."/".$product['product_id']."/".$picture['pa_path'];
			}else{
				$result = $picture['pa_path'];
			}
		}
		return $result;
	}
	
}












